package org.ycx.doc.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.ycx.doc.dao.auto.DocProjectSqlProvider;
import org.ycx.doc.dao.model.DocUserVersion;
import org.ycx.doc.dto.DocProjectInfo;
import org.ycx.doc.dto.DocUser;

import java.util.List;

public interface ProfileMapper {

    @Results({
            @Result(column = "UID", property = "uid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "LOGIN_NAME", property = "loginName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PASSWD", property = "passwd", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NICK_NAME", property = "nickName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "GENDER", property = "gender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MOBILE", property = "mobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "EMAIL", property = "email", jdbcType = JdbcType.VARCHAR),
            @Result(column = "AVATER", property = "avater", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PWD_END_DATE", property = "pwdEndDate", jdbcType = JdbcType.DATE),
            @Result(column = "USE_STATE", property = "useState", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DEL_STATE", property = "delState", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CREATE_TS", property = "createTs", jdbcType = JdbcType.DATE),
            @Result(column = "CREATE_UID", property = "createUid", jdbcType = JdbcType.INTEGER),
            @Result(column = "UPDATE_TS", property = "updateTs", jdbcType = JdbcType.DATE),
            @Result(column = "UPDATE_UID", property = "updateUid", jdbcType = JdbcType.INTEGER)
    })
    @Select("select * from sec_user where UID=#{uid}")
    DocUser findByUid(@Param("uid") int uid);

    @Update({
            "update sec_user",
            "set LOGIN_NAME = #{loginName,jdbcType=VARCHAR},",
            "NICK_NAME = #{nickName,jdbcType=VARCHAR},",
            "GENDER = #{gender,jdbcType=VARCHAR},",
            "MOBILE = #{mobile,jdbcType=VARCHAR},",
            "EMAIL = #{email,jdbcType=VARCHAR},",
            "AVATER = #{avater,jdbcType=VARCHAR} ",
            "where UID = #{uid,jdbcType=INTEGER}"
    })
    int updateUser(DocUser user);

    @Results({
            @Result(column = "APP_ID", property = "appId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "APP_NAME", property = "appName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "APP_DESC", property = "appDesc", jdbcType = JdbcType.VARCHAR),
            @Result(column = "APP_PROD_VERSION", property = "appProdVersion", jdbcType = JdbcType.INTEGER),
            @Result(column = "CREATE_TS", property = "createTs", jdbcType = JdbcType.DATE),
            @Result(column = "CREATE_UID", property = "createUid", jdbcType = JdbcType.INTEGER),
            @Result(column = "UPDATE_TS", property = "updateTs", jdbcType = JdbcType.DATE),
            @Result(column = "DEV_VERSION", property = "userVersion", jdbcType = JdbcType.INTEGER),
            @Result(column = "UPDATE_UID", property = "updateUid", jdbcType = JdbcType.INTEGER)
    })
    @Select("select dp.*,duv.DEV_VERSION  from doc_project dp,doc_user_version duv where duv.APP_ID = dp.APP_ID and duv.USER_ID=#{uid}")
    List<DocProjectInfo> findProjectByUid(@Param("uid") int uid);

    @Insert({
            "insert into doc_user_version (APP_ID,USER_ID, DEV_VERSION)",
            "values (#{appId},#{userId,jdbcType=INTEGER}, #{devVersion,jdbcType=INTEGER})"
    })
    int insertUserVersion(DocUserVersion duv);

    @Results({
            @Result(column = "APP_ID", property = "appId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "APP_NAME", property = "appName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "APP_DESC", property = "appDesc", jdbcType = JdbcType.VARCHAR),
            @Result(column = "APP_PROD_VERSION", property = "appProdVersion", jdbcType = JdbcType.INTEGER),
            @Result(column = "CREATE_TS", property = "createTs", jdbcType = JdbcType.DATE),
            @Result(column = "CREATE_UID", property = "createUid", jdbcType = JdbcType.INTEGER),
            @Result(column = "UPDATE_TS", property = "updateTs", jdbcType = JdbcType.DATE),
            @Result(column = "UPDATE_UID", property = "updateUid", jdbcType = JdbcType.INTEGER)
    })
    @Select("select * from doc_project where app_id=#{appId}")
    DocProjectInfo findProjectById(@Param("appId") Integer appId);

    @Update ("update doc_user_version set dev_version=#{userVersion} where app_id=#{appId} and user_id=#{uid}")
    int updateUserProject(@Param ("appId") Integer appId, @Param ("uid") int uid, @Param ("userVersion") Integer userVersion);

    @Delete ("delete from doc_user_version where app_id=#{appId} and user_id=#{uid}")
    int deleteUserApp(@Param ("appId") Integer appId, @Param ("uid") int uid);
}
